/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package study.alex.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author RERIR
 */
public class testPrepare {
    
    private Connection conn;
    private PreparedStatement pstm;

    public testPrepare(Connection conn) {
        this.conn = conn;
    }
    
    public void fillTable(String table, int n) throws SQLException {
        
        if (table.length() == 0 || n < 1) return;
        
        
        Statement st = conn.createStatement();
        
        st.execute("DROP TABLE " + table);
        st.execute("CREATE TABLE " + table + " (id INTEGER PRIMARY KEY, str VARCHAR(255), lenght INTEGER NOT NULL)");
//        st.execute("CREATE TABLE " + table + " (str VARCHAR(255) PRIMARY KEY, lenght INTEGER NOT NULL)");
        
        pstm = conn.prepareStatement("INSERT INTO " + table + " VALUES (?, ?, ?)");
//        pstm = conn.prepareStatement("INSERT INTO " + table + " VALUES (?, ?)");
                
        int q = 0;
        for (int i = 1; i <= n; i++) {
            String str = new randomString(255).nextString();
            pstm.setInt(1, i);
            pstm.setString(2, str);
            pstm.setInt(3, str.length());
/*            pstm.setString(1, str);
            pstm.setInt(2, str.length()); */
           
            
            q += pstm.executeUpdate();
        }
        
        System.out.println("Added " + q + " records");
    }
    
}
